package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.tool.ToolClient;
import com.fwtai.web.WelcomeDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 欢迎页统计
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-07-21 17:49:27
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class WelcomeService{

  private final WelcomeDao welcomeDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public WelcomeService(final WelcomeDao welcomeDao,final ThreadPoolTaskExecutor taskExecutor){
    this.welcomeDao = welcomeDao;
    this.taskExecutor = taskExecutor;
  }

  public String getAllUser(){
    return ToolClient.queryJson(welcomeDao.getAllUser());
  }

  public String getShareTotal(){
    return ToolClient.queryJson(welcomeDao.getShareTotal(LocalUserId.get()));
  }

  public String getRegisterMonth(){
    return ToolClient.queryJson(welcomeDao.getRegisterMonth());
  }

  public String getBrowseDay(final String day){
    if(day == null || day.length() <= 0){
      return ToolClient.queryJson(welcomeDao.getBrowseDay(null));
    }else{
      return ToolClient.queryJson(welcomeDao.getBrowseDay(day));
    }
  }

  public String getBrowseMonth(){
    return ToolClient.queryJson(welcomeDao.getBrowseMonth());
  }

  //合伙人-获取我的推广团队
  public String listShareInfo(PageFormData formData){
    try {
      formData = ToolClient.dataTableMysql(formData);
      if(formData == null)return ToolClient.jsonValidateField();
      formData.put("loginId",Long.valueOf(LocalUserId.get()));
      final List<Map<String,Object>> list = welcomeDao.getShareInfoList(formData);
      /*list.forEach(item ->{
        item.put("user_name",ToolString.replacePhone((String)item.get("user_name")));
      });*/
      return ToolClient.dataTableOK(list,welcomeDao.getShareInfoTotal(formData));
    } catch (final Exception e){
      return ToolClient.dataTableException();
    }
  }

  public String getDataOverview(){
    return CompletableFuture.supplyAsync(()->{
      try{
        final CompletableFuture<HashMap<String,Object>> mapData = CompletableFuture.supplyAsync(() -> welcomeDao.getDataOverview(),taskExecutor);
        final HashMap<String,Object> map = mapData.join();
        return ToolClient.queryJson(map);
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }
}